<?php
declare(strict_types=1);
// 商品分类
namespace app\tjy_v1\controller;
use think\facade\Db; //门面模式
use app\tjy_v1\model\GoodsCls as mysql; //导入模型
class GoodsCls
{
   
    //商品分类列表-JSON静态数据
    public function ListJSON()
    {
        $query = request()->param(); //请求参数
        // 写文件
        // file_put_contents('bb/web_api_' . date('Y-m-d_H-i-s', time()) . '.json',"pppp" . "\n", FILE_APPEND);
        // 文件地址为服务的运行目录/public/下
        if (empty($query['parent_id'])) {
            $file_path = 'json/goodscls000.json';
        } else {
            $file_path = 'json/goodscls' . $query['parent_id'] . ".json";
        }
        if (file_exists($file_path)) {
            // 文件内容读入
            $goodsclsdb = json_decode(file_get_contents($file_path), true);//将整个文件内容读入到一个字符串中-转数组
        } else {
            return json([
                'code' => 201,
                'time' => time(),
                'msg' => "读取数据文件错误",
                'db' => [],
            ]);
        }
        $apidb = [
            'code' => 200,
            'time' => time(),
            'msg' => "获取分类列表",
            'db' => $goodsclsdb,
        ];
        return json($apidb);
        $query = request()->param(); //请求参数
        if (isset($query['spread'])) {
            if ($query["spread"] == "off") {
                $cateTree = Db::name('admin__i_goods_cls_json')
                    ->where('name', 'goods_cls_spread_off')
                    ->find();
            } else {
                # 展开树格式...
                $cateTree = Db::name('admin__i_goods_cls_json')
                    ->where('name', 'goods_cls_spread_on')
                    ->find();
            }
            $txt_json = json_decode($cateTree['txt_json']);
            $txt_json_unicode = json_decode($cateTree['txt_json_unicode']);
            if ($txt_json == null || $txt_json == null) {
                $Tree = [
                    'code' => 201, //状态码
                    "msg" => '数据JSON处理错误',
                ];
            } else {
                $Tree = [
                    'code' => 200, //状态码
                    "msg" => $cateTree['msg'],
                    "name" => $cateTree['name'],
                    "txt_json" => $txt_json,
                    "txt_json_unicode" => $txt_json_unicode,
                ];
            }
        } else {
            $Tree = [
                'code' => 202, //状态码
                "msg" => '请求参数错误',
            ];
        }
        $apidb = [
            'msg' => '静态JSON分类树-此接口响应性能更好',
            'code' => 200, //状态码
            'cateTree' => $Tree,
            'query' => $query
        ];
        return json($apidb);
    }
}
